program myweibull, sortpreserve
	version 8.1
	if replay() {
		if (`"`e(cmd)'"' != "myweibull") error 301
		Replay `0'
	}
	else	Estimate `0'
end

program Estimate, eclass
	syntax varlist [if] [in]		///
		[fweight pweight] [,		///
		FAILure(varname numeric)	///
		noLOg noCONStant		/// -ml model- options
		Robust CLuster(varname)		///
		noLRTEST svy			///
		OFFset(varname numeric)		///
		EXPosure(varname numeric)	///
		Level(integer `c(level)')	/// -Replay- options
		HR				///
		*				/// -mlopts/svyopts- options
	]

	// mark the estimation sample
	marksample touse

	// check syntax
	if "`svy'" != "" {
		svymarkout `touse'
		// identify weight variable from -svyset-
		local wvar `r(weight)'
		svyopts modopts diopts options , `options'
		if "`s(subpop)'" != "" {
			local subpop `s(subpop)'
			local subopt subpop(`subpop')
		}
		local meff `s(meff)'
	}
	local diopts `diopts' level(`level') `hr'
	mlopts mlopts, `options'
	local cns `s(constraints)'
	gettoken time rhs : varlist
	if "`failure'" == "" {
		tempvar failure
		gen byte `failure' = 1
	}
	if "`cluster'" != "" {
		local clopt cluster(`cluster')
	}
	if "`weight'" != "" {
		tempvar wvar
		quietly gen double `wvar' `exp'
		local wgt "[`weight'=`wvar']"
	}
	if "`wvar'" != "" {
		// for initial value calculations
		local awgt "[aw=`wvar']"
	}
	if `"`robust'`cluster'`cns'"' != "" | "`weight'" == "pweight" {
		local lrtest nolrtest
	}
	if "`log'" != "" {
		local qui quietly
	}
	if "`offset'" != "" {
		local offopt "offset(`offset')"
	}
	if "`exposure'" != "" {
		local expopt "exposure(`exposure')"
	}

	// markout missing values from the estimation sample
	markout `touse' `wvar' `failure' `offset' `exposure' `subpop'
	markout `touse' `cluster', strok
	if "`subpop'" != "" {
		// restrict initial value calculations to
		// observations within the subpopulation
		local subtouse `touse' & `subpop' != 0
	}
	else	local subtouse `touse'

	if "`meff'" != "" {
		if "`constant'" == "" {
			// initial values for the misspecified model
			sum `time' if `subtouse', mean
			local b0 = r(mean)
			sum `failure' `awgt' if `subtouse', mean
			local b0 = ln(`b0'/r(mean))
			local initopt init(_cons=`b0') search(quietly)
		}

		`qui' di as txt _n "Fitting misspecified model:"
		ml model d2 myweibull_d2			///
			(lneta: `time' `failure' = `rhs',	///
				`constant' `offopt' `expopt'	///
			)					///
			/lngamma				///
			if `subtouse',				///
			`log' `mlopts' `initopt'		///
			missing maximize
		// covariance matrix from the misspecified model fit
		tempname Vmsp
		matrix `Vmsp' = e(V)
	}

	if "`constant'" == "" {
		// initial value
		sum `time' `awgt' if `subtouse', mean
		local b0 = r(mean)
		sum `failure' `awgt' if `subtouse', mean
		local b0 = ln(`b0'/r(mean))
		local initopt init(_cons=`b0') search(quietly)

		`qui' di as txt _n "Fitting constant-only model:"
		ml model d2 myweibull_d2			///
			(lneta: `time' `failure' = ,		///
				`offopt' `expopt'		///
			)					///
			/lngamma				///
			`wgt' if `touse',			///
			`log' `mlopts' `initopt'		///
			`svy' `subopt'				///
			nocnsnotes missing maximize
		if "`lrtest'" == "" {
			local contin continue search(off)
		}
		else {
			tempname b0
			mat `b0' = e(b)
			local contin init(`b0') search(off)
		}
	}

	// fit the full model
	`qui' di as txt _n "Fitting full model:"
	ml model d2 myweibull_d2			///
		(lneta: `time' `failure'= `rhs',	///
			`constant' `offopt' `expopt'	///
		)					///
		/lngamma				///
		`wgt' if `touse',			///
		`log' `mlopts' `robust' `clopt'		///
		`contin' `svy' `modopts'		///
		missing maximize

	if "`Vmsp'" != "" {
		// compute misspecification effects and save them to e()
		_svy_mkmeff `Vmsp'
	}
	ereturn scalar k_aux = 1
	ereturn local failure `failure'
	ereturn local depvar `time'
	// save a title for -Replay- and the name of this command
	ereturn local title "My Weibull estimates"
	ereturn local cmd myweibull

	Replay , `diopts'
end

program Replay
	syntax [, Level(integer `c(level)') hr * ]
	// use svyopts to get display options
	svyopts noopts diopts, `options'
	// anything in `noopts' will cause an error
	mlopts noopts, `noopts'
	ml display , level(`level') `diopts' `hr'	///
		diparm(lngamma, exp label("gamma"))	///
		diparm(lngamma,				///
			function(exp(-@))		///
			derivative(exp(-@))		///
			label("1/gamma")		///
		)
end
